import { defineStore } from 'pinia';

type fileType = 'md'

interface State {
  dirPath: string,
  activeFilePath: string,
  onlyPreview: boolean,
  fileType: fileType
}

const useFileStore = defineStore('userinfo', {
  state: (): State => ({
    dirPath: '',
    fileType: 'md',//编辑其类型
    onlyPreview: false,// 预览
    activeFilePath: ''
  }),
  actions: {
    // 修改文件路径
    updateDirPath(filePath: string) {
      // this.$reset();
      if (this.dirPath !== filePath) this.activeFilePath = ''
      this.dirPath = filePath;
    },
    // 修改将要操作的文件
    updateActiveFilePath(filePath: string) {
      this.activeFilePath = filePath;
    },
    // 修改文件类型
    updateFileType(type: fileType) {
      this.activeFilePath = '';
      this.fileType = type;
    },
    // 修改文件预览/编辑
    updateOnlyPreview() {
      this.onlyPreview = !this.onlyPreview
    }
  },
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'FILE',
        storage: localStorage,
        paths: ['dirPath', 'fileType']
      },
      {
        key: 'FILE',
        storage: sessionStorage,
        paths: ['activeFilePath', 'onlyPreview']
      }
    ]
  }
})

export default useFileStore;